<?php
include_once dirname(__FILE__)."/../../model/config.php";
		$_enseignant=new enseignant();
		$_ens_ue=new ens_ue();
if(isset($_POST["operation"])){
if($_POST["operation"]=="INSERT_ENS"){
		if(isset($_POST["cin"])){$_enseignant->set_cin($_POST["cin"]);}
		if(isset($_POST["nom_enseig"])){$_enseignant->set_nom_enseig($_POST["nom_enseig"]);}
		if(isset($_POST["prenom_enseig"])){$_enseignant->set_prenom_enseig($_POST["prenom_enseig"]);}
		if(isset($_POST["tel_mobile"])){$_enseignant->set_tel_mobile($_POST["tel_mobile"]);}
		if(isset($_POST["tel"])){$_enseignant->set_tel($_POST["tel"]);}
		if(isset($_POST["fax"])){$_enseignant->set_fax($_POST["fax"]);}
		if(isset($_POST["mail"])){$_enseignant->set_mail($_POST["mail"]);}
		if(isset($_POST["grade"])){$_enseignant->set_grade($_POST["grade"]);}
		if(isset($_POST["specialite"])){$_enseignant->set_specialite($_POST["specialite"]);}
		if(isset($_POST["code_universite"])){$_enseignant->set_code_universite($_POST["code_universite"]);}
		if(isset($_POST["code_etablissement"])){$_enseignant->set_code_etablissement($_POST["code_etablissement"]);}
		if(isset($_POST["login"])){$_enseignant->set_login($_POST["login"]);}
		if(isset($_POST["motpasse"])){$_enseignant->set_motpasse($_POST["motpasse"]);}
		
		/************************************************************************************/
		
		if(isset($_POST["code_ens"])){$_ens_ue->set_code_ens($_POST["code_ens"]);}
		if(isset($_POST["code_ue"])){$_ens_ue->set_code_ue($_POST["code_ue"]);}
		if(isset($_POST["role"])){$_ens_ue->set_role($_POST["role"]);}
		//if(isset($_POST["code_etab_rattach"])){$_ens_ue->set_code_etab_rattach($_POST["code_etab_rattach"]);}
		//if(isset($_POST["nbr_ens"])){$_ens_ue->set_nbr_ens($_POST["nbr_ens"]);}
		
		
		/*************************************************************************************/
		
		
		
		$res=$_enseignant->INSERT_ENS(); 

	   if($res!="success"){
		echo json_encode(array('success' => 'false','result' => "$res" ));
	  }else{
	  	$res=$_ens_ue->INSERT(); 
	  	echo json_encode(array('success' => 'true','result' => "$res" ));
	  	
	  }
}
elseif($_POST["operation"]=="DELETE_ENS"){
		if(isset($_POST["cin"])){
		$_enseignant->set_cin($_POST["cin"]);
		$res=$_enseignant->DELETE_ENS(); 

	   if($res!="success"){
		echo json_encode(array('success' => 'false','result' => "$res" ));
	  }else{
	  	echo json_encode(array('success' => 'true','result' => "$res" ));
	  	
	  }
		}
}

elseif($_POST["operation"]=="SELECT_ENS"){
	if(isset($_POST["code_ue"]) && !empty($_POST["code_ue"])){
		$res=$_enseignant->SELECT_ENS($_POST["code_ue"]); 
	
		 $data=array();
		  while($row=$_enseignant->fetch_object()){
		  	$data[]=$row;
	  	  }
  	 	 
		
	   if($res!="success"){
		echo json_encode(array('success' => 'false','result' => "$res" ));
	  }else{
	  	echo json_encode(array('totalcount' => count($data),'result' => $data ));
	  	
	  }
	}else{
		echo json_encode(array('success' => 'false','result' => "Code unité introuvable" ));
	}
}




elseif($_POST["operation"]=="UPDATE_ENS"){
		if(isset($_POST["cin"])){$_enseignant->set_cin($_POST["cin"]);}
		if(isset($_POST["nom_enseig"])){$_enseignant->set_nom_enseig($_POST["nom_enseig"]);}
		if(isset($_POST["prenom_enseig"])){$_enseignant->set_prenom_enseig($_POST["prenom_enseig"]);}
		if(isset($_POST["tel_mobile"])){$_enseignant->set_tel_mobile($_POST["tel_mobile"]);}
		if(isset($_POST["tel"])){$_enseignant->set_tel($_POST["tel"]);}
		if(isset($_POST["fax"])){$_enseignant->set_fax($_POST["fax"]);}
		if(isset($_POST["mail"])){$_enseignant->set_mail($_POST["mail"]);}
		if(isset($_POST["grade"])){$_enseignant->set_grade($_POST["grade"]);}
		if(isset($_POST["specialite"])){$_enseignant->set_specialite($_POST["specialite"]);}
		if(isset($_POST["code_universite"])){$_enseignant->set_code_universite($_POST["code_universite"]);}
		if(isset($_POST["code_etablissement"])){$_enseignant->set_code_etablissement($_POST["code_etablissement"]);}
		if(isset($_POST["login"])){$_enseignant->set_login($_POST["login"]);}
		if(isset($_POST["motpasse"])){$_enseignant->set_motpasse($_POST["motpasse"]);}
		$res=$_enseignant->UPDATE_ENS(); 

	   if($res!="success"){
		echo json_encode(array('success' => 'false','result' => "$res" ));
	  }else{
	  	echo json_encode(array('success' => 'true','result' => "$res" ));
	  	
	  }
}

}
else{
	$constraint='';$limit=0;$start=0;$pagination='';
	if(isset($_POST["cin"]) && !empty($_POST["cin"])){
		$constraint.=" AND cin='".$_POST["cin"]."'";
	}

	if(isset($_POST["nom_enseig"]) && !empty($_POST["nom_enseig"])){
		$constraint.=" AND nom_enseig='".$_POST["nom_enseig"]."'";
	}

	if(isset($_POST["prenom_enseig"]) && !empty($_POST["prenom_enseig"])){
		$constraint.=" AND prenom_enseig='".$_POST["prenom_enseig"]."'";
	}

	if(isset($_POST["tel_mobile"]) && !empty($_POST["tel_mobile"])){
		$constraint.=" AND tel_mobile='".$_POST["tel_mobile"]."'";
	}

	if(isset($_POST["tel"]) && !empty($_POST["tel"])){
		$constraint.=" AND tel='".$_POST["tel"]."'";
	}

	if(isset($_POST["fax"]) && !empty($_POST["fax"])){
		$constraint.=" AND fax='".$_POST["fax"]."'";
	}

	if(isset($_POST["mail"]) && !empty($_POST["mail"])){
		$constraint.=" AND mail='".$_POST["mail"]."'";
	}

	if(isset($_POST["grade"]) && !empty($_POST["grade"])){
		$constraint.=" AND grade='".$_POST["grade"]."'";
	}

	if(isset($_POST["specialite"]) && !empty($_POST["specialite"])){
		$constraint.=" AND specialite='".$_POST["specialite"]."'";
	}

	if(isset($_POST["code_universite"]) && !empty($_POST["code_universite"])){
		$constraint.=" AND code_universite='".$_POST["code_universite"]."'";
	}

	if(isset($_POST["code_etablissement"]) && !empty($_POST["code_etablissement"])){
		$constraint.=" AND code_etablissement='".$_POST["code_etablissement"]."'";
	}

	if(isset($_POST["login"]) && !empty($_POST["login"])){
		$constraint.=" AND login='".$_POST["login"]."'";
	}

	if(isset($_POST["motpasse"]) && !empty($_POST["motpasse"])){
		$constraint.=" AND motpasse='".$_POST["motpasse"]."'";
	}

	if(isset($_POST['limit'])){$limit=$_POST['limit'];}
	if(isset($_POST['start'])){$start=$_POST['start'];}
	if($limit>0){
		$pagination.=" LIMIT  $start,$limit"; 
}
		$res=$_enseignant->SELECT($constraint,$pagination); 

		 $data=array();
		  while($row=$_enseignant->fetch_object()){
		  	$data[]=$row;
	  	  }
  	 	 
		
	   if($res!="success"){
		echo json_encode(array('success' => 'false','result' => "$res" ));
	  }else{
	  	echo json_encode(array('totalcount' => count($data),'result' => $data ));
	  	
	  }}
?>